﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Grids;
using VAdvantage.Model;
using VAdvantage.Utility;
using VAdvantage.Dialogs;

namespace VAdvantage.Controls
{
    public partial class VStatusBar : UserControl
    {
        private SLControls.Controls.VComboBox cmdPage = null;

        public event SelectionChangedEventHandler SelectionChanged;   
        public VStatusBar()
        {
            InitializeComponent();
            helper = new SilverlightDesktop.Common.DoubleClickHelper(lblStatusDB);
            helper.DoubleClick += OnlblStatusDBDoubleClick;

            cmdPage = new SLControls.Controls.VComboBox();
            cmdPage.Background = new SolidColorBrush(Colors.White);
            cmdPage.BorderThickness = new Thickness(0);
            cmdPage.SelectionChanged += new SelectionChangedEventHandler(cmdPage_SelectionChanged);
            cmdPage.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            cmdPage.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            cmdPage.Margin = new Thickness(3);
        }

        private bool setting = false;
        void cmdPage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectionChanged != null && !setting)
            {
                SelectionChanged(sender, e);
            }
        }

        #region Declaration

        private SilverlightDesktop.Common.DoubleClickHelper helper;

        private bool mt_error;
        private String mt_text;
        //
        private String m_text;
        private DataStatusEvent _dse = null;

        ToolTip tp = new ToolTip();
        #endregion

        private Color _foreColor = Colors.Black;
        private SolidColorBrush foreColorBrush = new SolidColorBrush();
        public Color StatusLblForeColor
        {
            get { return _foreColor; }
            set
            {
                _foreColor = value;
                foreColorBrush.Color = _foreColor;
            }
        }

        private Color _errorColor = Colors.Red;
        private SolidColorBrush errorColorBrush = new SolidColorBrush();
        public Color ErrorForeColor
        {
            get { return _errorColor; }
            set
            {
                _errorColor = value;

                errorColorBrush.Color = _errorColor;
            }
        }

        private Color _DBForeColor = Colors.Black;
        private SolidColorBrush dbForeColorBrush = new SolidColorBrush();
        public Color DBForeColor
        {
            get { return _DBForeColor; }
            set
            {
                _DBForeColor = value;
                dbForeColorBrush.Color = _DBForeColor;
            }
        }

        /// <summary>
        ///Set Standard Status Line (non error)
        /// </summary>
        /// <param name="text"></param>
        public void SetStatusLine(String text)
        {
            if (text == null)
                SetStatusLine("", false);
            else
                SetStatusLine(text, false);
        }

        public void SetPageLine(string page,string total)
        {
            setting = true;
            int t = Convert.ToInt32(total);
            int p = Convert.ToInt32(page);
            //gingText.Text = page;
            if (t != cmdPage.Items.Count)
            {
                FillList(t);
            }
            if (cmdPage.SelectedIndex != (p - 1))
            {
                cmdPage.SelectedIndex = (p - 1);
            }

            setting = false;
            //crntPage.Text ="{ Page " + page;
           // totalPage.Text = total +"} ";
        }

        private void FillList(int total)
        {
            //List<int> lst = new List<int>();
            cmdPage.Items.Clear();
            for (int i = 0; i < total; i++)
            {
                cmdPage.Items.Add((i + 1).ToString());
            }
        }

        public void SetPageLine(DataStatusEvent e)
        {
            if (e == null)
                return;
            SetPageLine(e._currentPage.ToString(), e._totalPage.ToString());
        }

        /// <summary>
        ///Set Status Line
        /// </summary>
        /// <param name="text">text</param>
        /// <param name="error">error</param>
        public void SetStatusLine(String text, bool error)
        {
            mt_error = error;
            mt_text = text;
            if (mt_text != null && mt_text.Length > 0)
            {
                mt_text = mt_text.Replace("\n", " ");
                mt_text = mt_text.Replace("\r", " ");
            }


            //if (mt_error)
            //    lblStatusLine.Foreground = errorColorBrush;
            //else
            //    lblStatusLine.Foreground = foreColorBrush;

            //if (!Dispatcher.CheckAccess())
            //{
            //    Dispatcher.BeginInvoke(() =>
            //    {
            //        lblStatusLine.Text = " " + mt_text;

            //    });
            //}
            //else
            //{
                lblStatusLine.Text = " " + mt_text;
            //}
        }

        /// <summary>
        ///Get Status Line text
        /// </summary>
        /// <returns></returns>
        public String GetStatusLine()
        {
            return lblStatusLine.Text.Trim();
        }

        /// <summary>
        ///Set ToolTip of StatusLine
        /// </summary>
        /// <param name="tip"></param>
        public void SetStatusToolTip(String tip)
        {
            //tp.SetToolTip(lblStatusLine, tip);
            //lblStatusLine .setToolTipText(tip);
        }

        /// <summary>
        ///Set Status DB Info
        /// </summary>
        /// <param name="text"></param>
        /// <param name="dse">data status event</param>
        public void SetStatusDB(String text, DataStatusEvent dse)
        {
            //	log.config( "StatusBar.setStatusDB - " + text + " - " + created + "/" + createdBy);

            //if (Dispatcher.CheckAccess())
            //{
                if (text == null || text.Length == 0)
                {
                    lblStatusDB.Text = "";
                    lblStatusDB.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    //lblStatusDB.Dispatcher.BeginInvoke(
                    lblStatusDB.Text = " " + text + " ";
                    if (lblStatusDB.Visibility != System.Windows.Visibility.Visible)
                        lblStatusDB.Visibility = System.Windows.Visibility.Visible;
                }
                SetPageLine(dse);
            //}
            //else
            //{

            //    if (text == null || text.Length == 0)
            //    {
            //        Dispatcher.BeginInvoke(() =>
            //        {
            //            lblStatusDB.Text = "";
            //            lblStatusDB.Visibility = System.Windows.Visibility.Collapsed;
            //            SetPageLine(dse);
            //        });
            //    }
            //    else
            //    {
            //        Dispatcher.BeginInvoke(() =>
            //       {
            //           lblStatusDB.Text = " " + text + " ";
            //           if (lblStatusDB.Visibility != System.Windows.Visibility.Visible)
            //               lblStatusDB.Visibility = System.Windows.Visibility.Visible;
            //           SetPageLine(dse);
            //       });
            //    }

            //}

            //  Save
            m_text = text;
            _dse = dse;
        }

        /// <summary>
        ///Set Status DB Info
        /// </summary>
        /// <param name="text"></param>
        public void SetStatusDB(String text)
        {
            SetStatusDB(text, null);
        }

        /// <summary>
        ///Set Status DB Info
        /// </summary>
        /// <param name="no"></param>
        public void SetStatusDB(int no)
        {
            SetStatusDB(no.ToString(), null);
        }

        /// <summary>
        ///Set Info Line
        /// </summary>
        /// <param name="text"></param>
        public void SetInfo(String text)
        {
            if (Dispatcher.CheckAccess())
            {
                if (lblStatusLine.Visibility != System.Windows.Visibility.Visible)
                {
                    lblStatusLine.Visibility = System.Windows.Visibility.Visible;

                   

                    //lblInfo.AutoSize = true;
                    //this.Height = tblStatus.PreferredSize.Height;
                }
                lblStatusInfo.Text = text;
            }
            else
            {
                Dispatcher.BeginInvoke(() =>
                {
                    if (lblStatusLine.Visibility != System.Windows.Visibility.Visible)
                    {
                        lblStatusLine.Visibility = System.Windows.Visibility.Visible;
                        
                        //lblInfo.AutoSize = true;
                        //this.Height = tblStatus.PreferredSize.Height;
                    }
                    lblStatusInfo.Text = text;
                });
            }
        }

        public StackPanel PageNavigator
        {
            get
            {
                return pageNavigator;
            }
        }

        public SLControls.Controls.VComboBox Pages
        {
            get { return cmdPage; }
        }


        /// <summary>
        ///Get Combo Box
        /// </summary>
        /// <returns></returns>

        //public VAdvantage.Grids.VComboBox GetComboBox()
        //{
        //    return combo;
        //}

        /// <summary>
        ///Set ComboBox Items
        /// </summary>
        /// <param name="items"></param>
        //public void SetComboItems(Object[] items)
        //{
        //    for (int i = 0; i < items.Length; i++)
        //    {
        //        if (items[1] == null)
        //        {
        //            combo.Items.Add(DBNull.Value);
        //            continue;
        //        }
        //        combo.Items.Add(items[i]);
        //    }
        //    combo.Visible = items != null && items.Length > 0;
        //    tblCombo.Visible = combo.Visible;
        //}


        void OnlblStatusDBDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (_dse == null
                        || _dse.CreatedBy == null
                        || !MRole.GetDefault().IsShowPreference())
                return;
            //
            String title = Utility.Msg.GetMsg(Envs.GetContext(), "Who") + m_text;
            RecordInfo info = new RecordInfo(title, _dse);
            //Classes.ShowMessage.Info("Record_Id is  ", false, _dse.Record_ID.ToString(), "");
            //MessageBox.Show("Record_Id is  " + _dse.Record_ID.ToString());
            info.Show();
            //info = null;
        }

        public virtual void Dispose()
        {
            helper.DoubleClick -= OnlblStatusDBDoubleClick;
            helper.Dispose();
            helper = null;
            foreColorBrush = null;
            dbForeColorBrush = null;
            errorColorBrush = null;
            mt_text = null;
        }

        public TextBlock PagingText
        {
            get;
            set;
        }
    }
}